import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_wan_android/bean/article_list_bean.dart';
import 'package:flutter_wan_android/bean/project_list_bean.dart';
import 'package:flutter_wan_android/blocs/project_bloc.dart';
import 'package:flutter_wan_android/res/colors.dart';
import 'package:flutter_wan_android/res/dimens.dart';
import 'package:flutter_wan_android/ui/article/article_page.dart';
import 'package:flutter_wan_android/ui/project/new_project_page.dart';
import 'package:flutter_wan_android/ui/webview/webview_page.dart';
import 'package:flutter_wan_android/utils/navigator_utils.dart';
import 'package:flutter_wan_android/widgets/images.dart' as prefix0;

class ProgressView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
        alignment: Alignment.center,
        width: 20,
        height: 20,
        child: SizedBox(
          width: 20,
          height: 20,
          child: CircularProgressIndicator(
            strokeWidth: 2.0,
          ),
        ));
  }
}


enum Header{
  project,
  article,
}

class HeaderItemView extends StatelessWidget {
  final IconData leftIcon;
  final String leftTitle;
  final int leftColor;

  final Header header;

  const HeaderItemView(this.header,{this.leftIcon, this.leftTitle, this.leftColor, Key key, })
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
        height: 46,
        margin: EdgeInsets.only(top: 8.0),
        padding: EdgeInsets.only(left: 12, right: 5),
        decoration: BoxDecoration(
            color: XColors.itemHeaderBackgroundColor,
            border: Border(
                bottom: BorderSide(
              color: XColors.dividingLineColor,
              width: XDimens.dividingLine,
            ))),
        child: InkWell(
          onTap: (){
            switch(header){
              case Header.project:
                NavigatorUtils.pushProject(context, NewProjectPage());
                break;
              case Header.article:
                NavigatorUtils.pushArticle(context, ArticlePage());
                break;
            }
          },
          child: Flex(
            direction: Axis.horizontal,
            children: <Widget>[
              Expanded(
                flex: 1,
                child: Row(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Icon(
                      leftIcon,
                      color: leftColor == 0 ? XColors.black : Color(leftColor),
                    ),
                    Padding(padding: EdgeInsets.only(left: 8)),
                    Expanded(
                        flex: 1,
                        child: Text(
                          leftTitle,
                          style: TextStyle(
                              color: leftColor == 0
                                  ? XColors.black
                                  : Color(leftColor)),
                        ))
                  ],
                ),
              ),
              Icon(
                Icons.keyboard_arrow_right,
                size: 20,
              )
            ],
          ),
        )
    );
  }
}

class ProjectItemView extends StatelessWidget {
  final ProjectItem projectItem;

  const ProjectItemView(this.projectItem, {Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 160.0,
      padding: EdgeInsets.all(12.0),
      decoration: BoxDecoration(
          color: XColors.white,
          border: Border(
              bottom: BorderSide(
            color: XColors.dividingLineColor,
            width: XDimens.dividingLine,
          ))),
      child:InkWell(
        onTap: (){
          NavigatorUtils.push(context, WebViewPage(projectItem.projectLink, projectItem.title));
        },
        child:  Flex(
          direction: Axis.horizontal,
          children: <Widget>[
            Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(
                      projectItem.title,
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(fontWeight: FontWeight.bold, fontSize: 14.5),
                    ),
                    Padding(padding: EdgeInsets.only(bottom: 8.0)),
                    Expanded(
                      flex: 1,
                      child: Text(
                        projectItem.desc,
                        maxLines: 4,
                        style: TextStyle(color: XColors.itemContentBottomColor),
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: <Widget>[
                        CircleAvatar(
                          child: Icon(
                            Icons.tag_faces,
                            color: XColors.itemContentBottomColor,
                            size: 16,
                          ),
                          radius: 8,
                          backgroundColor: Colors.transparent,
                        ),
                        Padding(padding: EdgeInsets.only(left: 6)),
                        Text(
                          '${projectItem.author}    ${projectItem.niceShareDate}',
                          style: TextStyle(
                              fontSize: 13.0, color: XColors.itemContentBottomColor),
                        )
                      ],
                    )
                  ],
                )),
            Container(
                width: 72.0,
                height: 128.0,
                margin: EdgeInsets.only(left: 6),
                child: prefix0.NetworkImage(projectItem.envelopePic))
          ],
        ),
      ),
    );
  }
}

class ArticleItemView extends StatelessWidget {
  final ArticleItem item;

  const ArticleItemView(this.item,{Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 106,
      padding: EdgeInsets.all(12),
      decoration: BoxDecoration(
          color: XColors.white,
          border: Border(
              bottom: BorderSide(
            color: XColors.dividingLineColor,
            width: XDimens.dividingLine,
          ))),
      child: InkWell(
        onTap: (){
          NavigatorUtils.push(context, WebViewPage(item.link, item.title));
        },
        child: Flex(
          direction: Axis.horizontal,
          children: <Widget>[
            Expanded(

                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(
                      item.title,
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(fontWeight: FontWeight.bold, fontSize: 14.5),
                    ),
                    Padding(padding: EdgeInsets.only(bottom: 8.0)),
                    Expanded(
                      flex: 1,
                      child: Text(
                        item.desc,
                        maxLines: 2,
                        style: TextStyle(color: XColors.itemContentBottomColor),
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                    Padding(padding: EdgeInsets.only(bottom: 6.0)),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: <Widget>[
                        CircleAvatar(
                          child: Icon(
                            Icons.tag_faces,
                            color: XColors.itemContentBottomColor,
                            size: 16,
                          ),
                          radius: 8,
                          backgroundColor: Colors.transparent,
                        ),
                        Padding(padding: EdgeInsets.only(left: 4)),
                        Text(
                          '${item.shareUser.isEmpty?item.author:item.shareUser}',
                          style: TextStyle(
                              fontSize: 12.0, color: XColors.itemContentBottomColor),
                        ),
                        Padding(padding: EdgeInsets.only(left: 10)),
                        CircleAvatar(
                          child: Icon(
                            Icons.access_time,
                            color: XColors.itemContentBottomColor,
                            size: 16,
                          ),
                          radius: 8,
                          backgroundColor: Colors.transparent,
                        ),
                        Padding(padding: EdgeInsets.only(left: 4)),
                        Text(
                          '${item.niceShareDate}',
                          style: TextStyle(
                              fontSize: 12.0, color: XColors.itemContentBottomColor),
                        )
                      ],
                    )
                  ],
                )),
            Padding(padding: EdgeInsets.all(6)),
            ClipRRect(
              borderRadius: BorderRadius.circular(30.0),
              child: Container(
                alignment: Alignment.center,
                width: 60,
                height: 60,
                padding: EdgeInsets.only(left: 4,right: 4),
                decoration: BoxDecoration(color: randomColor()),
                child: Text(item.chapterName,
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    color: XColors.white,
                    fontSize: 12.0,

                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }


  Color randomColor() {
    return Color.fromARGB(255, Random().nextInt(256)+0, Random().nextInt(256)+0, Random().nextInt(256)+0);
  }


}

class FooterView extends StatelessWidget{
  final int height;

  const FooterView({Key key, this.height}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return new Container(
//      height: height==0?40:height,
      padding: EdgeInsets.all(10),
      alignment: Alignment.center,
      child:  Text('- 没有更多数据 -',style: TextStyle(
        color: XColors.itemContentBottomColor

    ),)

    );
  }

}
